package com.bjnet.castdemo.view.textureview;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Surface;
import android.view.TextureView;

public class AirUrlTextureView extends BaseTextureView implements TextureView.SurfaceTextureListener {

    public AirUrlTextureView(@NonNull Context context) {
        super(context);
    }

    public AirUrlTextureView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AirUrlTextureView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        getChannel().setSurface(new Surface(surface));
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int c) {
//        setVideoViewSize(width, c);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        getChannel().setSurface(null);
        return false;
    }


    private boolean count1 = true, count2 = true;

    @Override
    public void setDisplayViewCount(int count) {
        super.setDisplayViewCount(count);
        if (count == 1) {
            if(count2){
                setVideoViewSize(maxWidth,maxHeight);
                count1 = false;
            }else {
                textureView.setScaleX(1);
                textureView.setScaleY(1);
                count1 = false;
            }

        } else if (count == 2) {
            if(count1){
                textureView.setScaleX(1);
                textureView.setScaleY(0.5f);
                count2 = false;
            }  else {
                textureView.setScaleX(1);
                textureView.setScaleY(1);
            }
        }
    }
}
